package com.cy.teducommunitys.com.cy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.teducommunitys.com.common.annotation.RequiredLog;
import com.cy.teducommunitys.com.common.exception.ServiceException;
import com.cy.teducommunitys.com.cy.entity.BaseEntity;
import com.cy.teducommunitys.com.cy.service.IBaseService;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.*;

/**
 * service实现类的父类，实现简单的CRUD
 * 子类必须实现带参的构造方法
 * 参考CenterServiceImp
 * @param <T>
 */
@Slf4j
public class BaseServiceImpl<T>  extends ServiceImpl<BaseMapper<T>, T> implements IBaseService<T> {
    private BaseMapper<T> mapper;
    public BaseServiceImpl(BaseMapper<T> mapper){
        this.mapper = mapper;
    }


    /** 分页查询 */
    @RequiredLog("分页查询")
    @Override
    public IPage<T> findAllObjects(Page<T> page, HashMap<String,Object> map) {
        if(page.getCurrent()<1)
            throw new IllegalArgumentException("当前页码值不正确");
        page.setSize(6);
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        if(map != null && !map.isEmpty()) {
            Iterator it = map.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                Object val = map.get(key);
                if(val != null) {
                    if (val instanceof String) {
                        if (val != null && !"".equals(val))
                            wrapper.like(key, val);
                    } else {
                        HashMap<String, Object> cond = new HashMap<>();
                        cond.put(key, val);
                        wrapper.allEq(cond);
                    }
                }
            }
        }

        int rowCount=mapper.selectCount(wrapper);
        if(rowCount==0)
            throw new ServiceException("没有找到对应记录");

        return mapper.selectPage(page,wrapper);
    }

    /** 根据id查询 */
    @RequiredLog("根据id查询")
    @Override
    public T findObjectById(Integer id) {
        T entity = mapper.selectById(id);
        return entity;
    }

    /** 新增或修改，id为空则执行新增操作，否则执行修改操作 */
    @RequiredLog("保存")
    @Override
    public int saveObject(T entity,Integer id) {
        int rows = 0;
        if(id!=null && id>0){
            if(entity instanceof BaseEntity)
            {
                ((BaseEntity)entity).setModifiedUser("admin");
                ((BaseEntity)entity).setModifiedTime(new Date());
            }
            rows = mapper.updateById(entity);
        }
        else
        {
            if(entity instanceof BaseEntity)
            {
                ((BaseEntity)entity).setCreatedUser("admin");
                ((BaseEntity)entity).setCreatedTime(new Date());
            }
            rows = mapper.insert(entity);
        }

        if(rows == 0 && id == null)
            throw new ServiceException("保存失败");
        else if(rows == 0 && id != null)
            throw new ServiceException("更新失败");
        return rows;
    }

    /** 根据id删除*/
    @RequiredLog("删除")
    @Override
    public int deleteObject(Integer id) {
        if(id==null || id<=0)
            throw new ServiceException("请选择要删除的记录");
        int rows = mapper.deleteById(id);
        if(rows == 0)
            throw new ServiceException("记录可能已被删除");
        return rows;
    }
}
